//
//  MainViewController.swift
//  Stauts
//
//  Created by Kylin on 16/3/12.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
   // MARK:- 懒加载属性
    private lazy var imageNames : [String] = ["tabbar_home", "tabbar_message_center", "", "tabbar_discover", "tabbar_profile"]
    private lazy var composeBtn : UIButton = UIButton(imageName: "tabbar_compose_icon_add", bgimageName: "tabbar_compose_button")
    
    // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()

       setupComposeBtu()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        adjustItems()
    }

}

// MARK:- 设置UI 
extension MainViewController {
   /// 调整tabBarItems
    private func adjustItems() {
        
        for i in 0..<tabBar.items!.count {
            // 取出item
            let item = tabBar.items![i]
            
            // 判断下标如果是2那么不可以和用户进行交互
            if i == 2 {
                item.enabled = false
                continue
            }
            
            // 3.设置item的选中图片
            item.selectedImage = UIImage(named: imageNames[i] + "_highlighted")
        }
    }
     /// 添加发布按钮
    private func setupComposeBtu () {
        // 1.将按钮添加到tabbar中
        tabBar.addSubview(composeBtn)
       
        // 2: 设置发布按钮的位置
        composeBtn.center = CGPoint(x: tabBar.bounds.width * 0.5, y: tabBar.bounds.height * 0.5)
        // 监听按钮的点击
        composeBtn.addTarget(self, action: #selector(MainViewController.composeButonClick), forControlEvents: .TouchUpInside)
        
    }
}

extension MainViewController {
    @objc private func composeButonClick () {
       // 1:创建发布控制器
        let composeVc = ComposeViewController()
        
        // 2:包装成导航控制器
        let composeNav = UINavigationController(rootViewController: composeVc)
        
        // 3:弹出控制器
        presentViewController(composeNav, animated: true, completion: nil)
    }
}








